﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SharedGenomics.Workbench.Core;
using SharedGenomics.Domain;

namespace SharedGenomics.Workbench.Models
{
    public class DataFilterResultsModel : DataGridSource<EntityFilterSummary>, IDataFilterResultsModel
    {
        private bool _showIndividuals;
        private bool _showSnps;

        public DataFilterResultsModel()
        {
            this.Filter = efs => this.ShowSnps && efs.IsSnpSummary || this.ShowIndividuals && efs.IsIndividualSummary;
            this.ShowSnps = this.ShowIndividuals = true;
        }

        public EntityFilterSummary GetFilterSummaryById(string id)
        {
            return this.Where(efs => String.Equals(efs.Id, id, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
        }

        public IEnumerable<EntityFilterSummary> AllResults
        {
            get
            {
                return this.AllItems;
            }
        }

        public bool ShowSnps
        {
            get { return this._showSnps; }
            set
            {
                bool changing = this._showSnps ^ value;
                this._showSnps = value;

                if (changing)
                    this.ApplyFilter();
            }
        }

        public bool ShowIndividuals
        {
            get { return this._showIndividuals; }
            set
            {
                bool changing = this._showIndividuals ^ value;
                this._showIndividuals = value;

                if (changing)
                    this.ApplyFilter();
            }
        }

        public int ResultCount
        {
            get { return this.AllItems.Count; }
        }

        public int IndividualCount
        {
            get { return this.AllItems.Where(efs => efs.IsIndividualSummary).Count(); }
        }

        public int SnpCount
        {
            get { return this.AllItems.Where(efs => efs.IsSnpSummary).Count(); }
        }
    }
}
